<?php
class InvalidInputException extends Exception {
	
}

class HttpInputFilter {
	private $_data;
	public function __construct($source) {
		
		//avoid warning when HandlerBase::prepareParams returns false
		if (!is_array($source)) {
			$source = array();
		}
		$this->_data = $source;
	}
	
	public function getRaw($key) {
		if (!$this->hasKey($key)) {
            return NULL;
        }
        $v = $this->_data[$key];
        return $v;
	}
	
	public function getInt($key, $default = NULL) {
		if (!$this->hasKey($key)) {
            return $default;
        }
        $v = $this->_data[$key];
        return ScalarFilter::getInt($v);
	}
	public function getFloat($key, $default = NULL) {
		if (!$this->hasKey($key)) {
            return $default;
        }
        $v = $this->_data[$key];
        return ScalarFilter::getFloat($v);
	}
	
	public function getEmail($key) {
		if (!$this->hasKey($key)) {
            return NULL;
        }
        $v = $this->_data[$key];
        return ScalarFilter::getEmail($v);
	}
	
	public function getUrl($key) {
		if (!$this->hasKey($key)) {
            return NULL;
        }
        $v = $this->_data[$key];
        return ScalarFilter::getUrl($v);
	}
	
	
	public function getNoneEmpty($key, $default = NULL) {
		if (!$this->hasKey($key)) {
            return $default;
        }
        $v = $this->_data[$key];
        return ScalarFilter::getNoneEmpty($v);
	}
	
	public function getDateAsUnixTimestamp($key) {
		throw new Exception('Not implemented');
	}
	
	public function getDateAsString($key, $format) {
		throw new Exception('Not implemented');
	}
	
	public function getOneOf($key, $array) {
		if (!$this->hasKey($key)) {
            return NULL;
        }
        $v = $this->_data[$key];
        return ScalarFilter::getOneOf($v, $array);
	}
	
	public function getIntInRange($key, $lowerBound, $upperBound, $boundSpecifier = '[]') {
		if (!$this->hasKey($key)) {
            return NULL;
        }
        $v = $this->_data[$key];
		return ScalarFilter::getIntInRange($v, $lowerBound, $upperBound, $boundSpecifier);
	}
	
	public function getFloatInRange($key, $lowerBound, $upperBound, $boundSpecifier = '[]') {
		if (!$this->hasKey($key)) {
            return NULL;
        }
        $v = $this->_data[$key];
		return ScalarFilter::getFloatInRange($v, $lowerBound, $upperBound, $boundSpecifier);
	}
	
	/**
	 * Gets value matching custom regex
	 *
	 * @param string $reg Custom regular expression
	 */
	public function getCustom($key, $reg) {
		if (!$this->hasKey($key)) {
            return NULL;
        }
        $v = $this->_data[$key];
		return preg_match($reg, $v) ? $v : NULL;
	}
	
	public function hasKey($key) {
		return array_key_exists($key, $this->_data);
	}
	
	public static function assertNotNull()
	{
		$args = func_get_args();
		foreach ($args as $a) {
			if ($a === NULL) {
				return false;
			}
		}
		return true;
	}
}

class ScalarFilter {
	
	public static function getInt($val) {
        if (!Validator::isInt($val))
        	return NULL;
        return (int)$val;
	}
	public static function getFloat($val) {
        if (!Validator::isFloat($val))
        	return NULL;
        return (float)$val;
	}
	
	public static function getEmail($val) {
        if (!Validator::isEmail($val))
        	return NULL;
        return $val;
	}
	
	public static function getUrl($val) {
        if (!Validator::isURL($val))
        	return NULL;
        return $val;
	}
	public static function getNoneEmpty($val) {
        if (Validator::isEmpty($val))
        	return NULL;
        return $val;
	}
	public static function getOneOf($val, $array) {
        if (!in_array($val, $array))
        	return NULL;
        return $val;
	}
	
	private static function _getNumberInRange($val, $lowerBound, $upperBound, $boundSpecifier = '[]') {
		if (!Validator::inRange($val, $lowerBound, $upperBound, $boundSpecifier))
        	return NULL;
        return $val;
	}
	
	public static function getIntInRange($val, $lowerBound, $upperBound, $boundSpecifier = '[]') {
		return (int) (self::_getNumberInRange($val, $lowerBound, $upperBound, $boundSpecifier));
	}
	
	public static function getFloatInRange($val, $lowerBound, $upperBound, $boundSpecifier = '[]') {
		return (float) (self::_getNumberInRange($val, $lowerBound, $upperBound, $boundSpecifier));
	}
	
	public static function getDateAsUnixTimestamp($val) {
		throw new Exception('Not implemented');
	}
	
	public static function getDateAsString($val, $format) {
		throw new Exception('Not implemented');
	}
}

?>